<?php

namespace app\admin\controller;

use app\admin\controller\Admin;

/**
 * @title 前台会员
 */
class Member extends Admin {

    /**
     * @title 会员分组
     */
    public function group() {


        session('redirect', url('group') . '?' . http_build_query(request()->get()));

        $lists = model('member_group')->select();

        $this->assign('lists', $lists);

        builder('list')
                ->addItem('id', '#')
                ->addSortItem('listorder', '排序', 'member_group')
                ->addItem('title', '标题')
                ->addItem('icon', '图标', 'image')
                ->addItem('discount', '折扣')
                ->addItem('points', '积分要求')
                ->addAction('编辑', 'group_edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal openbox')
                ->addAction('删除', 'group_delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();
        return view();
    }

    /**
     * @导航分类添加
     */
    public function group_add() {

        if (request()->isPost()) {

            $post = request()->post();

            $insert_id = db('member_group')->insertGetId($post);

            if (is_numeric($insert_id)) {

                // 有排充的话就更新一下
                db('member_group')->where('id', $insert_id)->setField('listorder', $insert_id);


                $this->success('', session('redirect'));
            } else {
                $this->error('添加失败');
            }
        } else {
            builder('form')
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('discount', 'input', '折扣<font color="red">*</font>', '', 'lay-verify="required"', '', '例：95折请输入95')
                    ->addItem('points', 'input', '积分要求<font color="red">*</font>', '', 'lay-verify="required"', '', '达到该积分自动升级')
                    ->addItem('icon', 'image', '图标', '', 'lay-verify="required"', '', '用于显示在头像右上角')
                    ->build();
            return view();
        }
    }

    /**
     * @title 导航分类编辑
     */
    public function group_edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {
            $post = request()->post();

            if (db('member_group')->where('id', $id)->update($post) !== FALSE) {
                $this->success('', session('redirect'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $one = db('member_group')->where('id', $id)->find();
            builder('form')
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('discount', 'input', '折扣<font color="red">*</font>', '', 'lay-verify="required"', '', '例：95折请输入95')
                    ->addItem('points', 'input', '积分要求<font color="red">*</font>', '', 'lay-verify="required"', '', '达到该积分自动升级')
                    ->addItem('icon', 'image', '图标', '', 'lay-verify="required"', '', '用于显示在头像右上角')
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 导航分类删除
     */
    public function group_delete($id) {

        empty($id) && $this->error('参数不能为空');

        if (db('member_group')->where('id', $id)->delete() !== FALSE) {
            $this->success('', session('redirect'));
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 会员收货地址
     */
    public function address() {


        $lists = model('member_address')->model_where()->paginate(20, false, ['query' => request()->get()]);
        $this->assign('page', $lists->render());
        $this->assign('lists', $lists);


        builder('list')
                ->addItem('id', '#')
                ->addItem('nickname', '所属账号')
                ->addItem('name', '收件人')
                ->addItem('phone', '电话')
                ->addItem('province', '省')
                ->addItem('city', '市')
                ->addItem('county', '县/区')
                ->addItem('detail', '地址')
                ->addItem('update_time', '更新')
                ->addAction('编辑', 'address_edit', '<i class="layui-icon layui-icon-edit"></i>', 'openbox')
                ->build();

        return view();
    }

    /**
     * @title 编辑收货地址
     */
    public function address_edit($id) {



        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {


            $data['name'] = input('post.name', '');
            $data['phone'] = input('post.phone', '');

            $data['province'] = input('post.province', '');
            $data['city'] = input('post.city', '');
            $data['county'] = input('post.county', '');

            $data['detail'] = input('post.detail', '');

            $data['update_time'] = time();


            if (db('member_address')->where('id', $id)->update($data) !== FALSE) {
                $this->success('', url('address'));
            } else {
                $this->error('添加失败');
            }
        } else {

            $one = db('member_address')->where('id', $id)->find();


            builder('form')
                    ->addItem('name', 'input', '收件人<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('phone', 'input', '电话<font color="red">*</font>', '', 'lay-verify="required"', '', '联系电话')
                    ->addItem('citys', 'citys', '省市县(区)', ['province' => $one['province'], 'city' => $one['city'], 'county' => $one['county']])
                    ->addItem('detail', 'input', '详细地址<font color="red">*</font>', '', 'lay-verify="required"', '', '')
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 框架显示
     */
    public function iframe() {


        // 加载内容
        $this->assign('iframe_src', url('admin/member/index'));
        // 加载分类
        $this->assign('nodes', model('member_group')->iframe_category_json());
        return $this->fetch('base:iframe');
    }

    /**
     * @title 后台管理员列表
     */
    public function index() {


        $cid = input('get.category', null);

        session('redirect', url('index') . '?' . http_build_query(request()->get()));

        $lists = model('member')->model_where()->paginate(50, false, ['query' => request()->get()]);
        $this->assign('page', $lists->render());
        $this->assign('lists', $lists);


        builder('list')
                ->addItem('id', '#')
                ->addItem('group_text', '分组')
                ->addItem('username', '账号')
                ->addItem('nickname', '昵称')
                ->addItem('avatar', '头像', 'image')
                ->addItem('sex', '性别')
                ->addItem('create_time', '注册')
                ->addItem('update_time', '更新')
                ->addAction('编辑', 'edit', '<i class="layui-icon layui-icon-edit"></i>', 'openbox')
                ->build();

        return view();
    }

    /**
     * @title 批量删除
     */
    public function deletes() {
        $ids = input('post.ids/a');
        empty($ids) && $this->error('没有选择任何数据');

        $affect_rows = db('member')->where('id', 'in', $ids)->delete();
        if ($affect_rows) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 添加一个前台会员
     */
    public function add() {


        if (request()->isPost()) {

            $post = request()->post();

            $data['username'] = $post['username'] ?? '';
            $data['password'] = $post['username'] ?? '';
            $data['create_time'] = time();
            $data['update_time'] = time();

            // 用户名不能重复
            if (db('member')->where('username', $data['username'])->count()) {
                $this->error('用户名已经被注册了');
            }


            $data['password'] = my_md5($data['password']);

            if (db('member')->insert($data) !== FALSE) {
                $this->success('', url('index'));
            } else {
                $this->error('添加失败');
            }
        } else {
            builder('form')
                    ->addItem('group_id', 'select', '分组', model('member_group')->column('id,title'))
                    ->addItem('nickname', 'input', '昵称<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('username', 'input', '用户名<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('password', 'input', '密码<font color="red">*</font>', '', 'lay-verify="required"')
                    ->build();
            return view();
        }
    }

    /**
     * @title 导航编辑
     */
    public function edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {


            $data['username'] = input('post.username', '');
            $data['password'] = input('post.password', '');
            $data['update_time'] = time();

            if ($data['password']) {
                $data['password'] = my_md5($data['password']);
            }

            // 用户名不能重复
            if (db('member')->where('username', $data['username'])->where('id', '<>', $id)->count()) {
                $this->error('用户名已经被注册了');
            }

            if (db('member')->where('id', $id)->update($data) !== FALSE) {
                $this->success('', url('index'));
            } else {
                $this->error('添加失败');
            }
        } else {

            $one = db('member')->where('id', $id)->find();

            unset($one['password']);

            builder('form')
                    ->addItem('nickname', 'input', '昵称<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('username', 'input', '用户名<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('password', 'input', '密码', '', '', '', '如不修改，请留空')
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 分类转移 
     */
    public function set_category() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {
            $affect_rows = db('member')->where('id', 'in', $ids)->setField('group_id', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

}
